<?php

namespace app\store\controller\food\market\prepaid;

use app\store\controller\food\Controller;
use app\store\model\food\CardRule;
use app\store\model\food\Shop as ShopModel;
use think\facade\View;

class Card extends Controller
{
    /**
     * 列表
     */
    public function index()
    {
//        $model = new RechargePlanModel;
//        $list = $model->getList();
        $model = new CardRule();
        $list = $model->list();
        foreach($list as $k=>$v){
            $v['shop_name']= is_null($v['shop'])?'':$v['shop']['shop_name'];
            unset($v['shop']);
        }
        return View::fetch('index', compact('list'));
    }

    public function add()
    {
        if (!$this->request->isAjax()) {
            $modelShow = new ShopModel;
            $shoplist = $modelShow->getList(false);
            return View::fetch('add',compact("shoplist"));
        }
        $model = new CardRule();
        if ($model->add($this->postData('data'))) {
            return $this->renderSuccess('添加成功', url('food.market.prepaid.card/index'));
        }
        $error = $model->getError() ?: '添加失败';
        return $this->renderError($error);
    }

    public function delete($id)
    {
        $model = CardRule::get($id);
        if(empty($model['sku_id'])){
            $model["is_delete"]="1";
            $model->save();
            return $this->renderSuccess('删除成功');
        }else{
            return $this->renderError("已经绑定商品不支持删除规则");
        }
    }


    public function edit()
    {
        return View::fetch('edit');
    }

    /**
     * 预付卡规则绑定商品-列表
     */
    public function bind()
    {
        return View::fetch('bind');
    }
    /**
     * 预付卡规则绑定商品
     */
    public function bindadd()
    {
        return View::fetch('bind');
    }

}